package com.mhuang.common.coder;

import java.util.List;

import com.mhuang.utils.TypeConvert;
import com.mhuang.vo.BaseRespMessage;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

/**
 * 
 * @Package: com.mhuang.common.coder
 * @Description 消息解码
 * @author huang.miao
 * @date 2017年1月16日 下午2:42:41  
 * @since 1.0.0
 * @group skiper-opensource
 */
public class ClientDecoder extends ByteToMessageDecoder{

	//头消息长度（2+1）
	private final static int HEAD_LENGTH = 3;
	
	@Override
	protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
		
		if(in.readableBytes() < HEAD_LENGTH){
			return;
		}else{
			BaseRespMessage resp = new BaseRespMessage();
			byte[] msgArr = new byte[2];
			in.readBytes(msgArr);
			resp.setMsgId((int)TypeConvert.ByteArrayToLong(msgArr, 0, 2));
			resp.setResult(in.readByte());
			out.add(resp);
		}
	}
}
